package com.fjut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjut.mapper.VideoMapper;
import com.fjut.model.vod.Video;
import com.fjut.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjut.service.VodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

import static com.fjut.dateBase.VideoField.COURSE_ID;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author DengTianxun
 * @since 2023-03-06
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private VodService vodService;

    @Override
    public void removeVideoByCourseId(Long id) {
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        if (id == null) {
            return;
        }
        wrapper.eq(COURSE_ID,id);
        List<Video> videos = baseMapper.selectList(wrapper);
        videos.forEach(video -> {
            if (video.getVideoSourceId() != null) {
                vodService.deleteVideoById(video.getVideoSourceId());
            }
        });
        baseMapper.delete(wrapper);
    }

    @Override
    public Boolean removeVideoById(Long id) {
        Video video = baseMapper.selectById(id);
        if (!StringUtils.isEmpty(video.getVideoSourceId())) {
            vodService.deleteVideoById(video.getVideoSourceId());
        }
        int i = baseMapper.deleteById(id);
        return i > 0 ? true : false;
    }
}
